import {getessay ,putarticle,postfile,getfilename,Deletefile,editinfo , postvideo ,getattentions} from "@/service/main"
import {ElMessage} from 'element-plus'
import LocalCache from "@/utils/cache"

const main = {
  namespaced: true,
  state() {
    return {
      token: '',
      userInofo: {},
      UserMenus: [],
      contents:['hhhh'],
      z:0,
      c:0,
      essay:[],
      users:{},
      dialogVisible:false,
      filenames:[
        "xiaozhang.zip"
      ],
      data:{
        l:0,
        m:10
      }
    }
  },
  mutations: {
    //修改token
    changeToken(state, token) {
      state.token = token
    },
   setcontent(state, contents) {
    console.log(state, contents);
    state.contents = contents
   },
   changez(state, z){
    state.z = z
    console.log(z)
   },
   changec(state, c){
    state.c = c
   },
   changusers(state,data) {
    state.users = data
   },
   changessay(state,data) {
    state.essay =[...state.essay,...data] 
   },
   changfilename(state,filenames){
    state.filenames = filenames
   },
   changdata(state,data) {
    state.data.l= data.m
    state.data.m = data.m +10
   }
  },
  getters:{
    getusers:(state)=>{
      return state.users
    },
    getessay:(state)=>{
      return state.essay
    },
    getessayinfo:(state) =>{
      return (id)=>{
        return state.essay.filter((item)=>{
          return item.essay.uid == id
       })
      }
    }
  },
  actions: {
    async getcontent ({ commit }, payload) {
      //在这发送网络请求 拿到内容列表
      // const content = await 

      console.log(commit,payload);
    },
    async settoken ({ commit }, payload) {
      commit("changeToken",payload)
    },
    async getuserlists ({commit}) {
      console.log("diaoyon ");
      const userdata = [
        {
          id:1,
          name:"小张",
          img:require("@/assets/images/log.png"),
          tag:[
            "前端",
            "23岁",
            "打工仔"
          ],
          introduce:"打工人打工人打工人打工人打工人打工人打工人打工人"
        },
        {
          id:2,
          name:"效力",
          img:require("@/assets/images/log.png"),
          tag:[
            "前端",
            "23岁",
            "打工仔"
          ],
          introduce:"打工人打工人打工人打工人打工人打工人打工人打工人"
        },
        {
          id:3,
          name:"张三",
          img:require("@/assets/images/log.png"),
          tag:[
            "前端",
            "23岁",
            "打工仔"
          ],
          introduce:"打工人打工人打工人打工人打工人打工人打工人打工人"
        },
        {
          id:4,
          name:"小张",
          img:require("@/assets/images/log.png"),
          tag:[
            "前端",
            "23岁",
            "打工仔"
          ],
          introduce:"打工人打工人打工人打工人打工人打工人打工人打工人"
        },
        {
          id:5,
          name:"王武",
          img:require("@/assets/images/log.png"),
          tag:[
            "前端",
            "23岁",
            "打工仔"
          ],
          introduce:"打工人打工人打工人打工人打工人打工人打工人打工人"
        },
        {
          id:6,
          name:"里斯",
          img:require("@/assets/images/log.png"),
          tag:[
            "前端",
            "23岁",
            "打工仔"
          ],
          introduce:"打工人打工人打工人打工人打工人打工人打工人打工人"
        },
        {
          id:7,
          name:"小黑",
          img:require("@/assets/images/log.png"),
          tag:[
            "前端",
            "23岁",
            "打工仔"
          ],
          introduce:"打工人打工人打工人打工人打工人打工人打工人打工人"
        },
        {
          id:8,
          name:"小张  ",
          img:require("@/assets/images/log.png"),
          tag:[
            "前端",
            "23岁",
            "打工仔"
          ],
          introduce:"打工人打工人打工人打工人打工人打工人打工人打工人"
        }
      ]
      commit("changusers",userdata)
    },
    async getessay ({ commit ,state}) {
      let essaydata = await getessay(state.data)
      commit("changdata",state.data)
      console.log(essaydata,'essaydata');
      commit("changessay",essaydata)
    },
    async putarticle({commit},payload) {
      console.log(commit);
      await putarticle(payload)
    },
    //文件上传就在这上传啦 想着少一个模块 管理方便点 
    async postfile({commit},payload){
      console.log(commit,payload,'大文件上传');
      const res = await postfile(payload)
      if(res) {
        ElMessage.success({
          message:'文件上传成功'
        })
      }else {
        ElMessage.success({
          message:'文件上传失败',
          type:"error"
        })
      }
    },
    //视频文件上传 
    async putvideo ({commit},video) {
      console.log(commit);
         await postvideo(video)
    },
    //获取文件名称
    async getfilename({commit},payload) {
      console.log(commit,payload,'shuju ');
      const res = await getfilename(payload)
      console.log(res,'文件名称列表');
      commit('changfilename',res)
    },
    //文件删除 
    async Deletefile ({commit},payload) {
      const res = await Deletefile(payload)
      console.log(res,commit,'删除文件');
    },
    //用户信息修改 
    async editinfo({commit},payload) {
      const res = await editinfo({...payload})
      console.log(commit,res);
    },
    //查询关注人数
    async getattentions({commit},payload){
      const id = LocalCache.getCache('id')
      if(payload == 3) {
        const res = await getattentions(id)
      }
      console.log(payload,commit,'查询人数');
    }
  },
  modules: {}
}
export default main
